package com.nf.practice.distincttest;


import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class DisMapTest1 {

    @Test
    public void test1() {
//        Map<String, Map> msp = new HashMap<String, Map>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ruleVariableDesc", "101");
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ruleVariableDesc", "102");
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("ruleVariableDesc", "103");
        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("ruleVariableDesc", "102");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        System.out.println("初始数据：" + list.toString());
        System.out.println("去重之后：" + removeRepeatMapByKey(list,"id"));
    }

    /**
     * 根据map中的某个key 去除List中重复的map
     * @author  shijing
     * @param list
     * @param mapKey
     * @return
     */
    public List<Map<String, Object>> removeRepeatMapByKey(List<Map<String, Object>>
                                                                         list, String mapKey){
        if (CollectionUtils.isEmpty(list)) return null;

        //把list中的数据转换成msp,去掉同一id值多余数据，保留查找到第一个id值对应的数据
        List<Map<String, Object>> listMap = new ArrayList<>();
        Map<String, Map> msp = new HashMap<>();
        for(int i = list.size()-1 ; i>=0; i--){
            Map map = list.get(i);
            String id = (String)map.get(mapKey);
            map.remove(mapKey);
            msp.put(id, map);
        }
        //把msp再转换成list,就会得到根据某一字段去掉重复的数据的List<Map>
        Set<String> mspKey = msp.keySet();
        for(String key: mspKey){
            Map newMap = msp.get(key);
            newMap.put(mapKey, key);
            listMap.add(newMap);
        }
        return listMap;
    }


}
